// 项目视图模块
export function initProjectsView(views, projects, invoke) {
  return {
    updateProjectView,
    loadProjectTodos
  };

  // 更新项目视图
  async function updateProjectView() {
    const grid = document.getElementById('projects-grid');
    grid.innerHTML = '';

    projects.forEach(project => {
      const card = document.createElement('div');
      card.className = 'project-card';

      const header = document.createElement('div');
      header.className = 'project-header';

      const title = document.createElement('div');
      title.className = 'project-title';
      title.textContent = project.name;
      header.appendChild(title);

      const editButton = document.createElement('button');
      editButton.textContent = '编辑';
      editButton.addEventListener('click', () => showProjectModal(project));
      header.appendChild(editButton);

      card.appendChild(header);

      if (project.markdown_path) {
        const markdown = document.createElement('div');
        markdown.className = 'project-markdown';
        // TODO: 加载并渲染 Markdown 内容
        card.appendChild(markdown);
      }

      const todosContainer = document.createElement('div');
      todosContainer.className = 'project-todos';
      card.appendChild(todosContainer);

      // 加载项目最近的 Todo
      loadProjectTodos(project.id, todosContainer);

      grid.appendChild(card);
    });
  }

  // 加载项目 Todo
  async function loadProjectTodos(projectId, container) {
    try {
      const todos = await invoke('get_recent_todos_by_project', { project_id: projectId });
      container.innerHTML = '';

      todos.forEach(todo => {
        const todoElement = document.createElement('div');
        todoElement.className = `todo-item importance-${todo.importance.toLowerCase()}`;
        if (todo.completed) {
          todoElement.classList.add('completed');
        }

        const checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        checkbox.className = 'todo-checkbox';
        checkbox.checked = todo.completed;
        checkbox.addEventListener('change', () => toggleTodoCompletion(todo));
        todoElement.appendChild(checkbox);

        const content = document.createElement('div');
        content.className = 'todo-content';
        content.addEventListener('click', () => showTodoModal(todo));

        const title = document.createElement('div');
        title.className = 'todo-title';
        title.textContent = todo.title;
        content.appendChild(title);

        const meta = document.createElement('div');
        meta.className = 'todo-meta';

        if (todo.due_time) {
          const time = document.createElement('span');
          time.textContent = formatTime(new Date(todo.due_time));
          meta.appendChild(time);
        }

        content.appendChild(meta);
        todoElement.appendChild(content);
        container.appendChild(todoElement);
      });

      if (todos.length > 0) {
        const viewAll = document.createElement('button');
        viewAll.textContent = '查看全部';
        viewAll.addEventListener('click', () => {
          views.projects.classList.remove('active');
          views.tags.classList.add('active');
          // TODO: 过滤并显示该项目的所有 Todo
        });
        container.appendChild(viewAll);
      }
    } catch (error) {
      console.error('加载项目 Todo 失败:', error);
    }
  }

  // TODO: 需要从utils导入这些工具函数
  function formatTime(date) {
    return date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit',
    });
  }
}